//
//  ViewController.m
//  图片的相关处理
//
//  Created by Justsoso on 16/4/7.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"
#import "DrawView.h"

@interface ViewController ()
{
    CGPoint clipStartPoint;
    CGPoint clipEndPoint;
}
@property (weak, nonatomic) IBOutlet DrawView *drawView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic,strong) UIView *clipView;

@end

@implementation ViewController

- (UIView *)clipView
{
    if (_clipView == nil)
    {
        _clipView = [[UIView alloc] init];
        [self.imageView addSubview:_clipView];
        _clipView.backgroundColor = [UIColor blackColor];
        _clipView.alpha = 0.5;
    }
    return _clipView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)btnPressed:(id)sender
{
    /* 一、 得到UIImage对象 */
    /* 1. 创建位图上下文 （尺寸,透明: YES 透明 NO 不透明,拉伸） */
    UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, YES, 1);
    
    /* 2. 得到这个图像上下文 */
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    /* 3. 在该图像上下文中绘制 */
    
    /* 3.1 把视图上的layer绘制这个图像上下文中 */
    [self.drawView.layer renderInContext:ctx];
    
    
    /* 4. 生成图片 */
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    /* 5. 关闭图像上下文(释放内存,不关闭则会一直占用内存) */
    UIGraphicsEndImageContext();
    
    /* 二、 将UIImage存到沙盒中或相册中 */
    /* 1. 相册 */
//    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    
    /* 2. 沙盒 */
    /* 沙盒路径 */
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [path stringByAppendingPathComponent:@"冥王·雷利1.jpeg"];
    NSLog(@"%@",filePath);
    
    /* 2.1 生成png图片 */
//    NSData *imageData = UIImagePNGRepresentation(image);
//    [imageData writeToFile:filePath atomically:YES];
    /* 2.2 jpeg图片(需要设定压缩率0~1范围,数字越小,压缩的文件越小,越相对模糊) */
    NSData *imageData = UIImageJPEGRepresentation(image, 0.01);
    [imageData writeToFile:filePath atomically:YES];
    
    
}

- (IBAction)btn2Pressed:(id)sender
{
    /* 一、 得到UIImage对象 */
    /* 1. 创建位图上下文 （尺寸,透明: YES 透明 NO 不透明,拉伸） */
    UIGraphicsBeginImageContextWithOptions(self.drawView.bounds.size, YES, 1);
    
    /* 2. 得到这个图像上下文 */
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    /* 3. 在该图像上下文中绘制 */
    
    /* 3.2 自己添加路径进行绘制 */
    UIBezierPath *path1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(90, 20, 120, 120)];
    [[UIColor redColor] setFill];
    [path1 addClip];// 设置为裁剪路径
    
    /* 4. 生成图片 */
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    /* 5. 关闭图像上下文(释放内存,不关闭则会一直占用内存) */
    UIGraphicsEndImageContext();
    
    /* 二、 将UIImage存到沙盒中或相册中 */
    /* 1. 相册 */
    //    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    
    /* 2. 沙盒 */
    /* 沙盒路径 */
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [path stringByAppendingPathComponent:@"冥王·雷利1.jpeg"];
    NSLog(@"%@",filePath);
    
    /* 2.1 生成png图片 */
    //    NSData *imageData = UIImagePNGRepresentation(image);
    //    [imageData writeToFile:filePath atomically:YES];
    /* 2.2 jpeg图片(需要设定压缩率0~1范围,数字越小,压缩的文件越小,越相对模糊) */
    NSData *imageData = UIImageJPEGRepresentation(image, 0.01);
    [imageData writeToFile:filePath atomically:YES];
}
- (IBAction)btn3Pressed:(id)sender
{
    self.imageView.image = [UIImage imageNamed:@"2.jpg"];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    clipStartPoint = [touch locationInView:self.imageView];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.clipView.hidden = YES;
    /* 处理一下图片 */
    
    /* 1. 创建位图上下文 （尺寸,透明: YES 透明 NO 不透明,拉伸） */
    UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, YES, 1);
    
    /* 2. 得到这个图像上下文 */
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    /* 3. 在该图像上下文中绘制 */
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}
@end
